package com.itttq.gshop.admin.controller;

import java.beans.PropertyEditorSupport;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.SysUserPo;
import com.itttq.gshop.domain.po.WareBrandPo;
import com.itttq.gshop.domain.po.WareCategoryPo;
import com.itttq.gshop.domain.to.PageParamTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.connector.IWareBrandConnector;
import com.itttq.gshop.generic.connector.IWareCategoryConnector;
import com.itttq.gshop.generic.util.Constants;

public abstract class GenericController implements Serializable {
	private static final long serialVersionUID = -8751733063972470283L;
	@Autowired
	private IWareCategoryConnector wareCategoryConnector;
	@Autowired
	private IWareBrandConnector wareBrandConnector;
	
	/**
	 * 处理接收参数为""时转换为date类型校验不通过的问题
	 * 
	 * @param dataBinder
	 * @Author 迷蝶
	 * @return void
	 */
	@InitBinder  
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			public void setAsText(String value) {
				if(null == value) {
					setValue(null);
					return;
				}
				String formatStr = "yyyy-MM-dd";
				if(value.contains(" ")) {
					formatStr = "yyyy-MM-dd hh:mm:ss";
				}
				try {
					setValue(new SimpleDateFormat(formatStr).parse(value));
				} catch (ParseException e) {
					setValue(null);
				}
			}
			public String getAsText() {
				return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format((Date) getValue());
			}
		});
	}
	
	public void getAllBrands(ModelAndView mv) {
		WareBrandPo brands = new WareBrandPo();
		brands.setStart(0);
		brands.setSize(99999);
		mv.addObject("brandAll", wareBrandConnector.getWareBrands(brands).getData());
	}

	public void getAllCategories(ModelAndView mv) {
		WareCategoryPo cats = new WareCategoryPo();
		cats.setStart(0);
		cats.setSize(99999);
		mv.addObject("categoryAll", wareCategoryConnector.getWareCategorys(cats));
	}
	
	/**
	 * 从request中获取httpSession
	 * 
	 * @param request HttpServletRequest请求
	 * @return HttpSession对象
	 */
	public HttpSession getSession(HttpServletRequest request) {
		return request.getSession();
	}

	/**
	 * 设置值到session中
	 * 
	 * @param request request请求
	 * @param name session中名称
	 * @param value session中值
	 */
	public void setSession(HttpServletRequest request, String name, Object value) {
		this.getSession(request).setAttribute(name, value);
	}

	/**
	 * 设置值到session中
	 * 
	 * @param request request请求
	 * @param name session中名称
	 */
	public Object getSession(HttpServletRequest request, String name) {
		return this.getSession(request).getAttribute(name);
	}

	/**
	 * 获取分页参数，返回分页的Map对象
	 *
	 * @param request
	 * @return
	 * @auther <a href="mailto:gaoxvguo060@haopinfeng.com">高绪国</a> 2014-8-24
	 *         下午10:14:40
	 */
	public PageParamTo getPageParam(HttpServletRequest request) {
		String pageSizeStr = request.getParameter("ps");
		String pnStr = request.getParameter("pn");
		Integer ps = null,pn = null;
		if (!StringUtils.isEmpty(pageSizeStr)) {
			ps = Integer.parseInt(pageSizeStr);
		}
		if (!StringUtils.isEmpty(pnStr)) {
			pn = Integer.parseInt(pnStr);
		} else {
			pn = 1;
		}
		return getPageParam(pn, ps);
	}

	public PageParamTo getPageParam(Integer pn, Integer ps) {
		// String pageSizeStr = request.getParameter("ps");
		int start = 0;
		if (null == ps) {
			// 默认分页15页
			ps = 15;
		} else if (ps > 50) {
			ps = 50;
		}
		if (null != pn && pn.intValue() > 0) {
			start = (pn - 1) * ps;
		} else {
			pn = 1;
		}
		PageParamTo ppt = new PageParamTo();
		 ppt.setNo(pn);
		 ppt.setSize(ps);
		 ppt.setStart(start);
		return ppt;
	}

	public PageParamTo getPpt(int size) {
		PageParamTo ppt = new PageParamTo();
		ppt.setSize(size);
		return ppt;
	}
	
	public ModelAndView getPageMv(String attrName, 
			ModelAndView mv, ResultPageTo<?> rpt) {
		mv.addObject(attrName, rpt.getData());
		mv.addObject("count", rpt.getTotal());
		mv.addObject("start", rpt.getStart());
		mv.addObject("size", rpt.getSize());
		return mv;
	}
	/**
	 * 获取登录前的URL
	 * @param request
	 * @param url	如果没有登录前的URL时，跳转到此URL
	 * @return
	 */
	public String getLastUrl(HttpServletRequest request, String url) {
		Object lastUrl = this.getSession(request, Constants.SESSION_URL_LAST);
		// 清除上次地址
		this.getSession(request).removeAttribute(Constants.SESSION_URL_LAST);
		return null == lastUrl ? url : lastUrl.toString();
	}
	
	/**
	 * 获取用户对象
	 * @param request
	 * @return
	 */
	public SysUserPo getUserFromSession(HttpServletRequest request) {
		Object user = this.getSession(request, Constants.SESSION_USER);
		return null == user ? null :  (SysUserPo) user;
		
//		// TODO GXG 临时写死
//		SysUserPo sysUser = new SysUserPo();
//		sysUser.setId(1);
//		sysUser.setUsername("gshop");
//		return sysUser;
	}
}
